import { getTokenInfo } from '@/utils/storage'

export type GetToken = {
  token: string
  refresh_token: string
}
export type Profile = {
  usename: string
}
type userInfoType = {
  id: string
  name: string
  photo: string
  art_count: number
  follow_count: number
  fans_count: number
  like_count: number
}
export type PersonData = {
  id: string
  photo: string
  name: string
  mobile: string
  gender: number
  birthday: string
  real_name: string
  intro: string
}
const initState = {
  profile: {} as Profile,
  getToken: getTokenInfo() as GetToken,
  userInfo: {} as userInfoType,
  personData: {} as PersonData,
}

export type ActionType =
  | {
      type: 'user/getUserToken'
      payload: GetToken
    }
  | {
      type: 'user/getuserInfo'
      payload: userInfoType
    }
  | {
      type: 'user/getPersonData'
      payload: PersonData
    }
  | {
      type: 'user/updatePersonImg'
      payload: Partial<PersonData>
    }
  | {
      type: 'user/updateUserProfile'
      payload: Partial<PersonData>
    }
  | {
      type: 'user/clearAuth'
    }
export default function userReducer(state = initState, action: ActionType) {
  switch (action.type) {
    // 获取用户token
    case 'user/getUserToken':
      return { ...state, getToken: action.payload }
    // 清除用户的token
    case 'user/clearAuth':
      return { ...state, getToken: {} as GetToken }
    // 获取用户信息
    case 'user/getuserInfo':
      return { ...state, userInfo: action.payload }
    // 获取个人资料
    case 'user/getPersonData':
      return { ...state, personData: action.payload }
    //  更新用户头像
    case 'user/updatePersonImg':
      return { ...state, personData: { ...state.personData, ...action.payload } }
    // 更新用户信息
    case 'user/updateUserProfile':
      return { ...state, personData: { ...state.personData, ...action.payload } }

    default:
      return state
  }
}
